package com.example.smarterhouse;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.SparseArray;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;

public class RoomAndDeviceList extends Activity {
	SparseArray<Group> groups = new SparseArray<Group>();
	Controller controll = Controller.getInstance();
	Push push;
	int count = 0;
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.room_and_device_list, menu);
	    
	    return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle item selection
		Log.d("MENU", String.valueOf(item.getItemId()));
	    switch (item.getItemId()) {
	        case R.id.logoutButton:
	        	Log.d("LOGOUT", "FUCK");
	            controll.callAsync("logout_");
	            return true;
	        default:
	            return super.onOptionsItemSelected(item);

	    }
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_room_and_device_list);
		controll.setCon(this);
		controll.setCurrrentClass(RoomAndDeviceList.class);

		Log.d("FUCK", "FUCK");

//		ImageView logo = (ImageView)findViewById(R.id.imageView1);
//		logo.setOnClickListener(new View.OnClickListener() {
//			
//			@Override
//			public void onClick(View v) {
//				// TODO Auto-generated method stub
//				if(){
//				controll.setClassDest(lastLocation);
//		    	controll.setCon(con);
//		    	String alert = "push_"+controll.getRollBack()+"An Alarm has gone off";
//		    	controll.callAsync(alert);
//				controll.callAsync(alert);
//				}
//				}
//		});
		
		DisplayMetrics metrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metrics);
		ExpandableListView expand = (ExpandableListView)findViewById(R.id.listView);
		int height = metrics.heightPixels;
		height = height/3;
		RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
		lp.setMargins(15, 0, 15, height);
		expand.setLayoutParams(lp);
		
		
		Intent intent = getIntent();
		String devicesList = intent.getExtras().getString("devices");
		String roomsList = intent.getExtras().getString("rooms");
		
		String[] roomSplit = roomsList.split("_");
		String[] deviceSplit = devicesList.split("!");
		Log.d("DEVICESPLIT", deviceSplit[1]+"  "+deviceSplit[2]);
//		String[] strings = new String[8];
		List<List<String>> deviceIdList = new ArrayList();
		
		List<List<String>> listList = new ArrayList();
		for (int i = 1; i < deviceSplit.length; i++) {
			Log.d("i loop", devicesList);
			String[] strings = deviceSplit[i].split("_");
			Log.d("STRING LENGTH", String.valueOf(strings.length));
			int p = 0;
			List<String> idNumb = new ArrayList();

			 while	(p < strings.length) {
					idNumb.add(strings[p]);
					Log.d("DEVICE ID!!!", strings[p]);
					Log.d("P", String.valueOf(p));
					p =  p+2;
					
					
				}
			 p = 1;
			 List<String> name = new ArrayList();
				while ( p < strings.length) {
					
					name.add(strings[p]);
					p =  p+2;
				}
				deviceIdList.add(idNumb);
				listList.add(name);
				
			
		}

		Log.d("deviceIdList", String.valueOf(deviceIdList.get(0).size()));
		for (int i = 0; i < deviceIdList.size(); i++) {
			Log.d("RoomID ", String.valueOf(i));
			for (int j = 0; j < deviceIdList.get(i).size(); j++) {
				Log.d("Device Id LIST", deviceIdList.get(i).get(j));

			}
		}
		List<String> roomName = new ArrayList();
		
		

		int j = 1;
		String[] noRoomId = new String[roomSplit.length/2];
		for (int i = 0; i < noRoomId.length; i++) {
			
				noRoomId[i] = roomSplit[j];
				j = j+2;
			
		}

		Log.d("Rooms", roomsList);
		Log.d("Devices", devicesList);
		createData(noRoomId, listList);
	    ExpandableListView listView = (ExpandableListView) findViewById(R.id.listView);
	    ExpandableListAdapter adapter = new ExpandableListAdapter(this,groups, this, deviceIdList);
	    listView.setAdapter(adapter);
//	    dialog();
	}
	
	

	public void createData(String[] rooms, List<List<String>> listList) {
//		int j = 1;
		Log.d("ROOMS SIZE", String.valueOf(rooms.length));
	    for ( int j = 0; j < rooms.length; j++ ) {
	    	int counter = 0;
	    	Log.d("ROOM", rooms[j]);
	    	Log.d("ROOM", String.valueOf(j));
	      Group group = new Group(rooms[j]);
	      for (int i = 0; i < listList.get(j).size(); i++) {
	        group.children.add(listList.get(j).get(i));
	      }
	      Log.d("DAFUQ", group.string);
	      groups.append(j, group);
	      counter++;
//	      j = +2;
	    }
	    
	    
	  }
	
	
	

	
}
